package com.laosg.study.base.ch5

import java.lang.Thread

import scala.collection.mutable

/**
  * Created by kaimin on 9/5/2019.
  * time : 10:11
  * 特质，相当于接口和抽象类
  */
trait TraitTest {

  def getConnection()
}


class A {

}

class B extends A {

}

class cxas extends A with TraitTest {
  override def getConnection(): Unit = {
    println("连接mysql数据库")
  }
}

class D {

}

class E extends D {

}

class Fxx extends D with TraitTest {
  override def getConnection(): Unit = {
    println("连接oracle数据库")
  }
}

object getDiffConnect {
  def main(args: Array[String]): Unit = {
    var cc = new cxas
    var ff = new Fxx
    cc.getConnection()
    ff.getConnection()
    var a = new Array[Int](10)
    a(2) = 3
    for (i <- a) {
      println(i)
    }

    var abc = Array(1, 2)
    for (i <- abc) {
      println(i)
    }
    var tuplq = (1, 2, 3, "22")
    println(tuplq)

    println("---")
    println(tuplq._4)

    println(tuplq.productElement(2))
    println("---")
    for (i <- tuplq.productIterator) {
      println(i)
    }

    var ll = List(1, 2, 3)
    println(ll)
    ll.foreach(println)

    var mmm = Map(1 -> "a", 2 -> "b")
    println(mmm)
    mmm.foreach(println)
    println("---")
    var sd = mutable.Map((1, "xxx"), (2, "xcs"))
    sd(2) = "20"
    println("map" + sd)

    def test(f: Int => Int, a: Int) = {
      f(a)
    }

    def sum(a: Int) = {
      println(a + 2)
      a + 2
    }

    var res = test(sum, 3)
    println(res) //5

    def multi(a: Int): Int = {
      println("x 2被调用")
      2 * a
    }

    println("-----")
    var sc = ll.map(multi)
    println(sc)

    println("----")
    var names = List("abc", "wsc")


    var converted = names.map(convert)
    println(converted)
    println("---")
    val reverse = names.reverse

    println(reverse)
    println("---")
    println(reverse.flatMap(convert))
    println("--选出以a开头的元素-")
    println(reverse.filter(chose))


    var num = List(1, 2, 3, 4, 5)
    println("所有元素只和：" + num.sum)

    println("reduce求和：" + num.reduceLeft(suma))
    println("reduce相➖：" + num.reduceLeft(minus))
    println("reduce相you➖：" + num.reduceRight(minus))
    println("fold=" + num.foldLeft(6)(suma))
    println("fold---=" + num.foldLeft(0)(minus))
    println("fold---=" + num.foldRight(0)(minus))
    println("fold 缩写---=" + ((0 /: num) (minus)))
    println("fold 缩写---=" + ((num :\ 0) (minus)))
    println("scan=" + ((1 to 5).scanLeft(2)) (minus))

    val lines = List("atguigu han hello ", "atguigu han aaa aaa aaa ccc ddd uuu")

    var ress = lines.mkString(" ").split(" ").map((_, 1))
      .groupBy(_._1)
      .mapValues(_.map(_._2))
      .mapValues(_.reduce(_ + _))

    println("-----------")
    println("1:"+lines.mkString(" ").split(" ").map((_, 1))
      .groupBy(_._1).mapValues(_.map(v => (v._1,v._2+1))).foreach(d=>println(d._1+"  "+d._2)))

    println("-----------")
    println("计算单词个数：" + ress.foreach(println))


    var sx = List(1, 2, 3, 5)
    var sx2 = List(2, 3, 4)
    val sxc = sx.zip(sx2)
    println("拉链：" + sxc)
    for (item <- sxc) {
      println("拉链取值：" + item._1 + ":" + item._2)
    }


    def eq(i: Int): Boolean = {
      i.toString.equals(i.toString.reverse)
    }

    var pr = (1 to 100).filter(eq).sortWith(_.compareTo(_) > 0)
    println("1-100之内，逆序的" + pr)


    //    println("fen"+pr.par.foreach(println))

    print("---")
    val result1 = (0 to 100).map { case _ => Thread.currentThread.getName }.distinct
    print(result1)
    var result2 = (0 to 100).par.map { case _ => Thread.currentThread().getName }.distinct
    print(result2)

    println("-----")
    var n1 = 10
    var n2 = 2
    println(n1 + n2)

    println(n1.+(n2))

    n1 match {
      case 1 => {
        println("匹配到1")
      }
      case 2=>{
        println("匹配到2")
      }
      case _=>{
        println("默认匹配")
      }
    }


    var sset = mutable.Set(1,23,3)
    println("set:"+sset)
    var xsc = sset.+=(2,5)
    println("set:"+xsc)
    xsc-=2
    println("set -:"+xsc)

    var (x,y,z) = (1,2,"12")
    println("x="+x+" y="+y+" z="+z)



  }


  def put(ina: StringBuffer, inab: String): String = {
    ina.append(inab).toString
  }


  def convert(name: String): String = {
    name.toUpperCase
  }

  def chose(name: String): Boolean = {
    name.startsWith("a")
  }

  def suma(a: Int, b: Int): Int = {
    a + b
  }

  def minus(a: Int, b: Int): Int = {
    a - b
  }


}

